<?php
/**
 * Created by PhpStorm.
 * User: Inhere
 * Date: 15-4-1
 * Time: 上午10:08
 * Used:
 * file: Router.php
 */

namespace ulue\cli\router;

use Ulue;
use Windwalker\IO\Cli\Input;
use ulue\web\Module;
use ulue\core\router\AbstractRouter;

class Router extends AbstractRouter
{
    /**
     * 请求的路由
     * @var string
     */
    protected $routePath;

    /**
     * 请求的路由
     * @var array string[]
     */
    protected $route     = [
        'module'       => '',
        'controller'   => '',
        'action'       => '',
    ];


    public function __construct($routePath)
    {
        $this->routePath = $routePath;
    }

////////////////////////////////////////// route parse //////////////////////////////////////////

    /**
     * @return $this
     */
    public function execute()
    {
        $this->parseCliRoute();


        return $this;
    }

    public function parseCliRoute()
    {
        // 有路由信息
        if ( $this->routePath ) {
            $route   = $this->parseRoutePath($this->routePath);
            $this->routePath = implode('/', $route);

        // 访问默认页面
        } else {
            $default = Ulue::$app->get('default.module') ?: Ulue::$app->defaultModule;
            $route   = [ 'm' => $default, 'c' => '', 'a' => ''];
        }

        if ($route['m']) {
            $this->route['module'] = $route['m'];
        }

        //
        if ( !empty($route['c']) ) {
            $this->route['controller'] = $route['c'];
        }

        //
        if ( !empty($route['a']) ) {
            $this->route['action'] = $route['a'];
        }

    }

    /**
     * 开始解析路由字符串
     * @param $pathInfo
     * @return array
     */
    protected function parseRoutePath($pathInfo)
    {

        //路由信息 以默认的分隔符 ‘/’分割
        $sep     = '/';
        $urlInfo = !strpos($pathInfo,$sep) ?
                    array($pathInfo) :
                    array_map( function($value)
                    {
                        return trim($value, '/\\ ');
                    }, explode( $sep, $pathInfo)
                    );
        $urlInfo = array_filter($urlInfo);

        $modules = Module::names();
        $route   = [];

        list($m, $c, $a) = $vars = ['m', 'c', 'a'];

        #1 . 先检查存在可访问模块时
        if ( in_array( $urlInfo[0] , $modules ) ) {
            $i = 0;

            while ($urlInfo && $i<3) {
                $var         = $vars[$i];
                $route[$var] = array_shift($urlInfo);
                $i++;
            }
        #2 . 没有模块时 访问默认模块下的 控制器
        } else {
            $route[$m] = Ulue::$app->defaultModule;
            $route[$c] = array_shift($urlInfo);

            if ( isset( $urlInfo[0]) ) {
                $route[$a] = array_shift($urlInfo);
            }
        }

        return $route;
    }

    /**
     * @return string
     */
    public function getRoutePath()
    {
        return $this->routePath;
    }

    /**
     * @param string $routePath
     * @return $this
     */
    public function setRoutePath($routePath)
    {
        $this->routePath = $routePath;

        return $this;
    }
}